import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver } from '@angular/core';
import { ProductService } from './product.service';
import { PubProductPageComponent } from "../content/pub-product-page/pub-product-page.component";
import { OtmProductPageComponent } from "../content/otm-product-page/otm-product-page.component";
import { OtoProductPageComponent } from "../content/oto-product-page/oto-product-page.component";
import { AnnuityPageComponent } from "../content/annuity-page/annuity-page.component";
import { OldageProductPageComponent } from "../content/oldage-product-page/oldage-product-page.component";
import { SocialPageComponent } from "../content/social-page/social-page.component";
import { OldagePageComponent } from "../content/oldage-page/oldage-page.component";
import { AnnuityProductPageComponent } from "../content/annuity-product-page/annuity-product-page.component";
import { DataService } from '../../../services/data.service';
import * as _ from 'lodash';

@Component({
  selector: 'app-product',
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.scss'],
  providers: [ProductService]
})
export class ProductComponent implements OnInit {
  // 公募页面
  @ViewChild("pubProductPage", { read: ViewContainerRef })
  pubProductPage: ViewContainerRef;
  pubProductPageComponent: ComponentRef<PubProductPageComponent>;
  // 一对多页面
  @ViewChild("otmProductPage", { read: ViewContainerRef })
  otmProductPage: ViewContainerRef;
  otmProductPageComponent: ComponentRef<OtmProductPageComponent>;
  // 一对一页面
  @ViewChild("otoProductPage", { read: ViewContainerRef })
  otoProductPage: ViewContainerRef;
  otoProductPageComponent: ComponentRef<OtoProductPageComponent>;
  // 企业年金页面
  @ViewChild("annuityPage", { read: ViewContainerRef })
  annuityPage: ViewContainerRef;
  annuityPageComponent: ComponentRef<AnnuityPageComponent>;
  // 养老金产品页面
  @ViewChild("oldageProductPage", { read: ViewContainerRef })
  oldageProductPage: ViewContainerRef;
  oldageProductPageComponent: ComponentRef<OldageProductPageComponent>;
  // 社保基金页面
  @ViewChild("socialPage", { read: ViewContainerRef })
  socialPage: ViewContainerRef;
  socialPageComponent: ComponentRef<SocialPageComponent>;
  // 养老基金页面
  @ViewChild("oldagePage", { read: ViewContainerRef })
  oldagePage: ViewContainerRef;
  oldagePageComponent: ComponentRef<OldagePageComponent>;
  // 养老金部综合页面
  @ViewChild("annuityProductPage", { read: ViewContainerRef })
  annuityProductPage: ViewContainerRef;
  annuityProductPageComponent: ComponentRef<AnnuityProductPageComponent>;
  // lodash
  public _: any = _;
  // 菜单数据
  public menuData: any;
  // 菜单按钮集合
  public menuResourceSet: any;
  //tab权限code集合
  public menuCodeList: any;

  constructor(
    public productService: ProductService,
    public resolver: ComponentFactoryResolver,
    public dataService: DataService
  ) {

  }

  ngOnInit() {
    let that = this;
    // 权限处理
    new Promise((resolve, reject) => {
      this.dataService.initTree.subscribe((data) => {
        if (data) {
          let SessionMenuData = sessionStorage.getItem('menu');
          this.menuData = JSON.parse(SessionMenuData)
          this.menuResourceSet = [];
          _.forEach(this.menuData, (item, index) => {
            if (item.sName == '综合查询') {
              _.forEach(item.children, (it, idx) => {
                if (it.sName == '产品信息综合查询') {
                  that.menuResourceSet = it.resourceSet
                  sessionStorage.setItem('btn-productInfo', this.menuResourceSet);
                }
              })
            }
          })
        }
      })
      let SessionMenuData = sessionStorage.getItem('menu');
      this.menuData = JSON.parse(SessionMenuData)
      this.menuResourceSet = [];
      _.forEach(this.menuData, (item, index) => {
        if (item.sName == '综合查询') {
          _.forEach(item.children, (it, idx) => {
            if (it.sName == '产品信息综合查询') {
              that.menuResourceSet = it.resourceSet
              console.log(that.menuResourceSet)
              sessionStorage.setItem('btn-productInfo', this.menuResourceSet);
            }
          })
        }
      })
      resolve(that.menuResourceSet);
    }).then(data => {
      if (that.menuResourceSet.length > 0) {
        switch (that.menuResourceSet[0]) {
          case 'tab-productInfo-product-pub':
            window['$']('#pub').addClass('active');
            window['$']('#gm').addClass('active');
            this.showPubProductPage();
            break;
          case 'tab-productInfo-product-otm':
            window['$']('#otm').addClass('active');
            window['$']('#ydd').addClass('active');
            this.showOtmProductPage();
            break;
          case 'tab-productInfo-product-oto':
            window['$']('#oto').addClass('active');
            window['$']('#ydy').addClass('active');
            this.showOtoProductPage();
            break;
          case 'tab-productInfo-product-annuityPro':
            window['$']('#annuityPro').addClass('active');
            window['$']('#annuitypro').addClass('active');
            this.showannuityProductPage('');
            break;
          case 'tab-productInfo-product-QYNJ':
            window['$']('#QYNJ').addClass('active');
            window['$']('#qynj').addClass('active');
            this.showAnnuityPage('4');
            break;
          case 'tab-productInfo-product-YLJCP':
            window['$']('#YLJCP').addClass('active');
            window['$']('#yljcp').addClass('active');
            this.showOldageProductPage('5');
            break;
          case 'tab-productInfo-product-SHBJJ':
            window['$']('#SHBJJ').addClass('active');
            window['$']('#shbjj').addClass('active');
            this.showSocialPage('6');
            break;
          case 'tab-productInfo-product-YLJJ':
            window['$']('#YLJJ').addClass('active');
            window['$']('#yljj').addClass('active');
            this.showOldagePage('7');
            break;
        }
      }
    })
  }
  // 公募页面
  showPubProductPage() {
    if (this.pubProductPageComponent && this.pubProductPageComponent.instance) {
      this.pubProductPageComponent.destroy();
    }
    const pubProductPageComponent = this.resolver.resolveComponentFactory(PubProductPageComponent);
    this.pubProductPageComponent = this.pubProductPage.createComponent(pubProductPageComponent);
  }
  // 一对多页面
  showOtmProductPage() {
    if (this.otmProductPageComponent && this.otmProductPageComponent.instance) {
      this.otmProductPageComponent.destroy();
    }
    const otmProductPageComponent = this.resolver.resolveComponentFactory(OtmProductPageComponent);
    this.otmProductPageComponent = this.otmProductPage.createComponent(otmProductPageComponent);
  }
  // 一对一页面
  showOtoProductPage() {
    if (this.otoProductPageComponent && this.otoProductPageComponent.instance) {
      this.otoProductPageComponent.destroy();
    }
    const otoProductPageComponent = this.resolver.resolveComponentFactory(OtoProductPageComponent);
    this.otoProductPageComponent = this.otoProductPage.createComponent(otoProductPageComponent);
  }
  // 养老金部综合页面
  showannuityProductPage(sFundType: any) {
    if (this.annuityProductPageComponent && this.annuityProductPageComponent.instance) {
      this.annuityProductPageComponent.destroy();
    }
    const annuityProductPageComponent = this.resolver.resolveComponentFactory(AnnuityProductPageComponent);
    this.annuityProductPageComponent = this.annuityProductPage.createComponent(annuityProductPageComponent);
    this.annuityProductPageComponent.instance.getBody.sFundType = sFundType;
  }
  // 企业年金页面
  showAnnuityPage(sFundType: any) {
    if (this.annuityPageComponent && this.annuityPageComponent.instance) {
      this.annuityPageComponent.destroy();
    }
    const annuityPageComponent = this.resolver.resolveComponentFactory(AnnuityPageComponent);
    this.annuityPageComponent = this.annuityPage.createComponent(annuityPageComponent);
    this.annuityPageComponent.instance.getBody.sFundType = sFundType;
  }
  // 养老金产品页面
  showOldageProductPage(sFundType: any) {
    if (this.oldageProductPageComponent && this.oldageProductPageComponent.instance) {
      this.oldageProductPageComponent.destroy();
    }
    const oldageProductPageComponent = this.resolver.resolveComponentFactory(OldageProductPageComponent);
    this.oldageProductPageComponent = this.oldageProductPage.createComponent(oldageProductPageComponent);
    this.oldageProductPageComponent.instance.getBody.sFundType = sFundType;
  }
  // 社保基金页面
  showSocialPage(sFundType: any) {
    if (this.socialPageComponent && this.socialPageComponent.instance) {
      this.socialPageComponent.destroy();
    }
    const socialPageComponent = this.resolver.resolveComponentFactory(SocialPageComponent);
    this.socialPageComponent = this.socialPage.createComponent(socialPageComponent);
    this.socialPageComponent.instance.getBody.sFundType = sFundType;
  }
  // 养老基金页面
  showOldagePage(sFundType: any) {
    if (this.oldagePageComponent && this.oldagePageComponent.instance) {
      this.oldagePageComponent.destroy();
    }
    const oldagePageComponent = this.resolver.resolveComponentFactory(OldagePageComponent);
    this.oldagePageComponent = this.oldagePage.createComponent(oldagePageComponent);
    this.oldagePageComponent.instance.getBody.sFundType = sFundType;
  }
}
